document.addEventListener('DOMContentLoaded', () => {
    // 初始化 Markdown 编辑器
    const easyMDE = new EasyMDE({
        element: document.getElementById('markdown-editor'),
        spellChecker: false,
        status: false,
        placeholder: '在这里输入笔记内容...',
        autofocus: false,
        toolbar: [
            'bold', 'italic', 'heading', '|',
            'quote', 'code', 'unordered-list', 'ordered-list', '|',
            'link', 'image', 'table', '|',
            'preview', 'side-by-side', 'fullscreen', '|',
            'guide'
        ]
    });

    // 设置默认的开始和结束时间
    const now = new Date();
    const later = new Date(now.getTime() + 60 * 60 * 1000); // 默认时长1小时
    
    document.getElementById('start-time').value = formatDateTimeForInput(now);
    document.getElementById('end-time').value = formatDateTimeForInput(later);

    // 保存按钮点击事件
    document.getElementById('save-note').addEventListener('click', saveNote);

    // 保存笔记
    function saveNote() {
        const noteData = {
            title: document.getElementById('note-title').value,
            content: easyMDE.value(),
            startTime: document.getElementById('start-time').value,
            endTime: document.getElementById('end-time').value,
            priority: document.getElementById('priority').value,
            status: document.getElementById('status').value,
            assignee: document.getElementById('assignee').value
        };

        // 验证必填字段
        if (!noteData.title.trim()) {
            alert('请输入笔记标题');
            document.getElementById('note-title').focus();
            return;
        }

        if (!noteData.startTime || !noteData.endTime) {
            alert('请选择开始和结束时间');
            return;
        }

        // 验证开始时间不能大于结束时间
        const startTime = new Date(noteData.startTime);
        const endTime = new Date(noteData.endTime);
        if (startTime > endTime) {
            alert('开始时间不能大于结束时间');
            return;
        }

        if (!noteData.assignee.trim()) {
            alert('请输入负责人');
            document.getElementById('assignee').focus();
            return;
        }

        // 这里应该发送数据到后端保存
        console.log('创建新笔记:', noteData);
        
        // 创建成功后返回列表页面
        window.location.href = 'index.html';
    }

    // 格式化日期时间为input标签所需的格式
    function formatDateTimeForInput(date) {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        return `${year}-${month}-${day}T${hours}:${minutes}`;
    }
}); 